home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio__filbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.3 KB  |  57 lines

  1. /*            _ f i l b u f
  2.  *
  3.  * Allocate and fill a stream IO buffer. This function is
  4.  * intimately tied to the macro getc() in the stdio library.
  5.  *
  6.  * The function will get a buffer full of data then return
  7.  * the first character. EOF is returned on error.
  8.  *
  9.  * Patchlevel 1.1
  10.  *
  11.  * Edit History:
  12.  * 05-Sep-1989    Add EINTR repeat code after read().
  13.  */
  14.  
  15. #include "stdiolib.h"
  16.  
  17. /*LINTLIBRARY*/
  18.  
  19. int _filbuf(fp)
  20.  
  21. FILE *fp;                /* stream */
  22.  
  23. {
  24.   int bytes;                /* bytes read */
  25.   int read();                /* read from channel */
  26.  
  27.   if (GETFLAG(fp, (_IORW | _IOREAD | _IOWRITE)) == _IORW)
  28.     SETFLAG(fp, _IOREAD);
  29.  
  30.   if (GETFLAG(fp, (_IOREAD | _IOERR | _IOEOF | _IOSTRING)) != _IOREAD)
  31.     return EOF;
  32.  
  33. /* Locate buffer space for this stream */
  34.   if (fp->_base == NULL && _allocbuf(fp) < 0) {
  35.     SETFLAG(fp, _IOERR);
  36.     return EOF;
  37.   }
  38.  
  39. /* Flush stdout if we're reading from stdin */
  40.   if (fp == stdin && TESTFLAG(stdout, _IOLBF))
  41.     (void) fflush(stdout);
  42.  
  43. /* Read data into the buffer */
  44.   do
  45.     bytes = read(fp->_file, (char *) fp->_base,
  46.          TESTFLAG(fp, _IONBF) ? 1 : fp->_bufsiz);
  47.   while (bytes == -1 && errno == EINTR);
  48.  
  49.   INITREADBUFFER(fp, bytes == -1 ? 0 : bytes);
  50.  
  51.   switch (bytes) {
  52.   case -1: SETFLAG(fp, _IOERR); return EOF;
  53.   case 0:  SETFLAG(fp, _IOEOF); return EOF;
  54.   default:                      return *fp->_ptr++;
  55.   }
  56. }
  57.